/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.taxigang.client.css;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.LinkElement;
import com.google.gwt.user.client.Window;
import com.taxigang.client.TaxiGangProcessor;
import com.taxigang.client.facebook.FBConnection;

/** Manages creating {@link FBConnection}s to receive messages from the server. */
public class CSSFactory {

	private static boolean scriptLoaded = false;
	private static String cssSrc;

	//public static FBConnection createFBConnection() {
	public static void loadCSS(String css_src) {
		cssSrc = css_src;
		if (css_src != null){
			Log.info("css loading:"+css_src);
			if (!scriptLoaded) {
				LinkElement style = Document.get().createLinkElement();
				style.setHref(css_src);
				style.setType("text/css");
				style.setRel("stylesheet");
				Document.get().getElementsByTagName("head").getItem(0).appendChild(style);
				addOnLoadHandler(style);
				//cssReady();

			}
		} else {
			cssReady();
		}

		//return createChannelImpl(channelId);
	}

//	if (filetype=="js"){ //if filename is a external JavaScript file
//		  var fileref=document.createElement('script')
//		  fileref.setAttribute("type","text/javascript")
//		  fileref.setAttribute("src", filename)
//		 }
//		 else if (filetype=="css"){ //if filename is an external CSS file
//		  var fileref=document.createElement("link")
//		  fileref.setAttribute("rel", "stylesheet")
//		  fileref.setAttribute("type", "text/css")
//		  fileref.setAttribute("href", filename)
//		 }
//		 if (typeof fileref!="undefined")
//		  document.getElementsByTagName("head")[0].appendChild(fileref)
//		}
	public static native void addOnLoadHandler(LinkElement script)/*-{
    		script.onload = function() {
           		@com.taxigang.client.css.CSSFactory::cssReady()();
			}
    		script.onLoad = function() {
           		alert("onLoad");
			}
    		script.onerror = function() {
           		@com.taxigang.client.css.CSSFactory::cssError()();
			}
			// IE 6 & 7
			script.onreadystatechange = function() {
			if (this.readyState == 'complete') {
           		@com.taxigang.client.css.CSSFactory::cssReady()();
			}
			}

    	}-*/;

	public static  void cssReady(){
		Log.info("css ready");
		scriptLoaded = true;
		//FacebookApi.fbGetSession(thisCallback);
		TaxiGangProcessor.continueTaxigang();

	}

	public static  void cssError(){
		Window.alert("css load error on :"+cssSrc);
		TaxiGangProcessor.continueTaxigang();

	}

}

